using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using Firestarter.DB.Schema.Logic;
using Habanero.Testability;
using NUnit.Framework;

namespace Firestarter.DB.Schema.Test.Logic
{
    [TestFixture]
    public class TestRegexBuilderSQL
    {
        
        private static string GetRandomString()
        {
            return RandomValueGen.GetRandomString();
        }
        [Test]
        public void Test_BeginCreateTable()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string tableName = GetRandomString();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.BeginCreateTable(tableName).ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(@"CREATE\s+TABLE\s+" + tableName + @"\s+\(\s*", regex);
        }

        [Test]
        public void Test_BeginCreateTable_Match_Pass()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string tableName = GetRandomString();
            string sql = "CREATE TABLE " + tableName + " (";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.BeginCreateTable(tableName).ToString();
            //---------------Test Result -----------------------
            Assert.IsTrue(Regex.IsMatch(sql, regex));
        }

        [Test]
        public void Test_BeginCreateTable_Match_Fail_BadSpelling()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string tableName = GetRandomString();
            string sql = "CEATE TABLE " + tableName + " (";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.BeginCreateTable(tableName).ToString();
            //---------------Test Result -----------------------
            Assert.IsFalse(Regex.IsMatch(sql, regex));
        }

        [Test]
        public void Test_BeginCreateTable_Match_Fail_NoTable()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string tableName = GetRandomString();
            string sql = "CEATE TABLE  (";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.BeginCreateTable(tableName).ToString();
            //---------------Test Result -----------------------
            Assert.IsFalse(Regex.IsMatch(sql, regex));
        }

        [Test]
        public void Test_BeginCreateTable_Match_Fail_BracketMissing()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string tableName = GetRandomString();
            string sql = "CEATE TABLE " + tableName + " ";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.BeginCreateTable(tableName).ToString();
            //---------------Test Result -----------------------
            Assert.IsFalse(Regex.IsMatch(sql, regex));
        }

        [Test]
        public void Test_EndCreateTable()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.EndCreateTable().ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(@"\)\s*", regex);
        }

        [Ignore("//TODO Mark 18 Jan 2009: (from brett this is broken not sure why")]
        [Test]
        public void Test_DataType()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.DataType().ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(@"\w+(|\((\d+(|,\d+){0,1}){0,1}\))", regex);
        }

        [Test]
        public void Test_DataType_Match_Pass()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string sql = "Integer";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.StartText.DataType().EndText.ToString();
            //---------------Test Result -----------------------
            Assert.IsTrue(Regex.IsMatch(sql, regex));
        }

        [Test]
        public void Test_DataType_Match_Pass_WithParam()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string sql = "VARCHAR(34)";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.StartText.DataType().EndText.ToString();
            //---------------Test Result -----------------------
            Assert.IsTrue(Regex.IsMatch(sql, regex));
        }

        [Test]
        public void Test_DataType_Match_Pass_WithDoubleParam()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string sql = "Number(10,2)";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.StartText.DataType().EndText.ToString();
            //---------------Test Result -----------------------
            Assert.IsTrue(Regex.IsMatch(sql, regex));
        }

        [Test]
        public void Test_DataType_Match_Fails_WithTripleParam()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string sql = "Number(10,2,3)";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.StartText.DataType().EndText.ToString();
            //---------------Test Result -----------------------
            Assert.IsFalse(Regex.IsMatch(sql, regex));
        }

        [Test]
        public void Test_ColumnDefinition()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.ColumnDefinition().ToString();
            //---------------Test Result -----------------------
            string expected = @"\w+\s+" + RegexBuilderSQL.Create().DataType().ToString();
            Assert.AreEqual(expected, regex);
        }

        [Test]
        public void Test_ColumnDefinition_Match_Pass()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string sql = "myColumn Integer";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.ColumnDefinition().ToString();
            //---------------Test Result -----------------------
            Assert.IsTrue(Regex.IsMatch(sql, regex));
        }

        [Test]
        public void Test_ColumnDefinition_Match_Pass_WithParam()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string sql = "myColumn VARCHAR(34)";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.ColumnDefinition().ToString();
            //---------------Test Result -----------------------
            Assert.IsTrue(Regex.IsMatch(sql, regex));
        }

        [Test]
        public void Test_ColumnDefinition_Match_Pass_WithDoubleParam()
        {
            //---------------Set up test pack-------------------
            RegexBuilderSQL regexBuilderSQL = new RegexBuilderSQL();
            string sql = "myColumn Number(10,2)";
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string regex = regexBuilderSQL.ColumnDefinition().ToString();
            //---------------Test Result -----------------------
            Assert.IsTrue(Regex.IsMatch(sql, regex));
        }
        
    }
}
